package com.jingdun.sport.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jingdun.sport.authority.SecurityChecker;
import com.jingdun.sport.bean.model.*;
import com.jingdun.sport.security.util.SecurityUtils;
import com.jingdun.sport.service.SyntheticPrizeService;
import com.jingdun.sport.service.UserPrizeWidgetRelationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/sport/userPrizeWidgetRelation/")
@Api(tags = "用户奖品部件关联关系接口")
public class UserPrizeWidgetRelationController {

    @Autowired
    SecurityChecker securityChecker;

    @Autowired
    private SyntheticPrizeService syntheticPrizeService;

    @Autowired
    private UserPrizeWidgetRelationService userPrizeWidgetRelationService;

//    /**
//     * 新增用户奖品部件关联关系接口
//     */
//    @PostMapping("/addUserPrizeWidgetRelation")
//    @ApiOperation(value = "新增用户奖品部件关联关系接口", notes = "新增用户奖品部件关联关系接口")
//    public ResponseEntity<ApiResult> addUserPrizeWidgetRelation(@RequestBody UserPrizeWidgetRelation userPrizeWidgetRelation) {
//        userPrizeWidgetRelationService.addUserPrizeWidgetRelation(userPrizeWidgetRelation);
//        ApiResult apiResult = new ApiResult();
//        return ResponseEntity.ok(apiResult);
//    }

    /**
     * 获取用户奖品部件关联关系接口
     */
    @GetMapping("/getUserPrizeWidgetRelationDetail")
    @ApiOperation(value = "获取用户奖品部件关联关系接口", notes = "获取用户奖品部件关联关系接口")
    public ApiResult getUserPrizeWidgetRelationDetail(long id) {
        Map m = new HashMap<String, Object>();
        m.put("id", id);
        UserPrizeWidgetRelation userPrizeWidgetRelation = userPrizeWidgetRelationService.getUserPrizeWidgetRelationDetail(m);
        ApiResult apiResult = new ApiResult();
        apiResult.setData(userPrizeWidgetRelation);
        return apiResult;
    }

    /**
     * 删除用户奖品部件关联关系接口
     */
    @PostMapping("/removeUserPrizeWidgetRelation")
    @ApiOperation(value = "删除用户奖品部件关联关系接口", notes = "删除用户奖品部件关联关系接口")
    public ResponseEntity removeUserPrizeWidgetRelation(long id) {
        UserPrizeWidgetRelation userPrizeWidgetRelation = new UserPrizeWidgetRelation();
        userPrizeWidgetRelation.setId(id);
        userPrizeWidgetRelation.setEnabled("0");
        userPrizeWidgetRelationService.updateUserPrizeWidgetRelation(userPrizeWidgetRelation);
        ApiResult apiResult = new ApiResult();
        //return ResponseEntity.ok().build();
        return ResponseEntity.ok(apiResult);
    }

    /**
     * 分页获取用户奖品部件关联关系接口
     */
    @RequestMapping("/getPaginationUserPrizeWidgetRelation")
    @ApiOperation(value = "分页获取用户奖品部件关联关系接口", notes = "分页获取用户奖品部件关联关系接口")
    public ResponseEntity<ApiResult> getPaginationUserPrizeWidgetRelation(long size, long current) {
        Map m = new HashMap<String, Object>();
        m.put("size", size);
        m.put("current", current);
        Page<Map<String, Object>> page = userPrizeWidgetRelationService.getPaginationUserPrizeWidgetRelation(m);
        ApiResult apiResult = new ApiResult();
        apiResult.setData(page);
        return ResponseEntity.ok(apiResult);
    }

    /**
     * 获取我的背包相关数据接口
     */
    @GetMapping("/getMyPackageDataList")
    @ApiOperation(value = "获取我的背包相关数据接口", notes = "获取我的背包相关数据接口")
    public ResponseEntity getMyPackageDataList() {
        Map m = new HashMap<String, Object>();
        //m.put("userId", "0cz0ajnfx088js3b6h97b41dwn9340po");
        m.put("userId", securityChecker.getUser().getUserId());
//        List list = userPrizeWidgetRelationService.getMyPackageDataList(m);
        List<PareGetMyPackageDataListResponse> list = userPrizeWidgetRelationService.getMyPackageDataList(securityChecker.getUser().getUserId());
        NewGetMyPackageDataListResponse newGetMyPackageDataListResponse = new NewGetMyPackageDataListResponse();
        newGetMyPackageDataListResponse.setPareGetMyPackageDataListResponseList(list);
        LambdaQueryWrapper<SyntheticPrize> syntheticPrizeLambdaQueryWrapper = new LambdaQueryWrapper<SyntheticPrize>()
                .eq(SyntheticPrize::getUserId, SecurityUtils.getUser().getUserId());
        List<SyntheticPrize> syntheticPrizeList = syntheticPrizeService.list(syntheticPrizeLambdaQueryWrapper);
        if (syntheticPrizeList != null && !syntheticPrizeList.isEmpty()) {
            List<EasySyntheticPrice> easySyntheticPriceList = new ArrayList<>(syntheticPrizeList.size());
            for (SyntheticPrize syntheticPrize : syntheticPrizeList) {
                EasySyntheticPrice easySyntheticPrice = new EasySyntheticPrice();
                easySyntheticPrice.setSyntheticPrizeId(syntheticPrize.getId());
                easySyntheticPrice.setStatus(syntheticPrize.getStatus());
                easySyntheticPrice.setPic(syntheticPrize.getPrizePic());
                easySyntheticPrice.setName(syntheticPrize.getPrizeName());
                easySyntheticPriceList.add(easySyntheticPrice);
            }
            newGetMyPackageDataListResponse.setEasySyntheticPriceList(easySyntheticPriceList);
        }
        return ResponseEntity.ok(newGetMyPackageDataListResponse);
    }
}